# -*- coding: utf-8 -*-

import re

link_and_name_re = re.compile(r'<a href="([^"]+)" class="searchable"  title="([^"]+)"')
ids_re = re.compile(r'<tr id="row_(\d+)" class="item row">')
results_number_re = re.compile(ur'Найдено(?: более)?\&\#032;(\d+) человек')
spaces_re = re.compile(r'\s+')

def parse(content):
  links_and_names = [(i[0], spaces_re.sub(' ', i[1]).strip())
                     for i in link_and_name_re.findall(content)]
  
  ids = [int(i) for i in ids_re.findall(content)]
  people = [{"i": i[0],
             "h": i[1][0],
             "n": i[1][1]} for i in zip(ids, links_and_names)]
  
  return {"count": int(results_number_re.findall(content)[0]),
          "results": people}
